package com.yicong.ants.ui.me

import android.view.View
import androidx.core.view.isVisible
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ToastUtils
import com.cchao.simplelib.core.RxHelper
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter
import com.yicong.ants.R
import com.yicong.ants.bean.me.FansResp
import com.yicong.ants.databinding.ActivityFansListBinding
import com.yicong.ants.databinding.ItemFansBinding
import com.yicong.ants.manager.AppHelper
import com.yicong.ants.manager.http.RetrofitHelper
import com.yicong.ants.view.MyDecoration

class FansListActivity : BaseTitleBarActivity<ActivityFansListBinding>(), View.OnClickListener {

    override fun getLayout(): Int {
        return R.layout.activity_fans_list
    }

    private var page = 1
    private lateinit var mAdapter: StatefulBindQuickAdapter<FansResp>

    override fun initEventAndData() {
        setTitleText("我的粉丝")
        setTitleBarVisible(false)
        AppHelper.transStatus(window)
        BarUtils.setStatusBarLightMode(this, false)
        mDataBind.click = this

        mDataBind.recycler.layoutManager = LinearLayoutManager(mContext)
        val dec =  MyDecoration(this, MyDecoration.VERTICAL_LIST)
        dec.setPadding(12)
        mDataBind.recycler.addItemDecoration(dec)
        mAdapter = object : StatefulBindQuickAdapter<FansResp>(R.layout.item_fans, null, false) {
            override fun loadPageData(page: Int) {
            }

            override fun convert(helper: DataBindViewHolder, item: FansResp?) {
                (helper.binding as ItemFansBinding).fansData = item
            }
        }
        mAdapter.bindToRecyclerView(mDataBind.recycler)

        mDataBind.smartLayout.setOnRefreshListener {
            page = 1
            loadPage(page)
        }

        mDataBind.smartLayout.setOnLoadMoreListener() {
            loadPage(page)
        }

        loadPage(page)
    }

    override fun onLoadData() {

    }

    fun loadPage(page: Int) {
        if (page == 1) {
            mDataBind.smartLayout.setEnableLoadMore(true)
        }
        val params: MutableMap<String, String> = HashMap()
        params["page"] = page.toString()
        addSubscribe(
            RetrofitHelper.getApis().directChildren2(params)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    mDataBind.smartLayout.finishRefresh()
                    mDataBind.smartLayout.finishLoadMore()
                    val data = respBean.data
                    if (page == 1) {
                        mAdapter.setNewData(data)
                        mDataBind.emptyView.isVisible = data.isNullOrEmpty()
                        mDataBind.recycler.isVisible = !data.isNullOrEmpty()
                    } else {
                        if (!data.isNullOrEmpty()) {
                            mAdapter.addData(data)
                        }
                    }
                    if (data.isNullOrEmpty()) {
                        mAdapter.loadMoreEnd()
                    } else {
                        mAdapter.loadMoreComplete()
                    }
                    this.page++
                    if (respBean.isCodeFail) {
                        ToastUtils.showLong(respBean.msg)
                    }
                }, {
                    ToastUtils.showLong(it.message)
                    if (mAdapter.data.isEmpty()) {
                        mDataBind.emptyView.isVisible = true
                    }
                })
        )
    }

    override fun onClick(view: View?) {
        when (view!!.id) {
            R.id.back -> {
                finish()
            }

            R.id.exchangeBtn -> {
                mDataBind.emptyView.isVisible = false
                mDataBind.recycler.isVisible = true
                mDataBind.smartLayout.autoRefresh()
            }
        }
    }
}